package com.glasses.business.pay.com;

import android.app.Activity;

import com.glasses.business.entity.PayInfo;
import com.glasses.business.pay.alipay.AlipayHelper;
import com.glasses.business.pay.wxpay.WechatPayHelper;

/**
 * @Description: 支付控件
 * @author wang
 * @date 2016-3-10 下午6:12:37
 * @version V1.0
 */
public class PayAgent {

//	private String notifyUrl="http://121.40.35.3/test"; //回调的url
	private String notifyUrl="http://malimall.net:8080/malimao_admin/Server/pay/IRecordPayOfWeixin"; //回调的url
	private String notifyIp="127.0.0.1"; //回调的ip

	private boolean isInit;
	private static AlipayHelper mAlipayHelper;
	private static WechatPayHelper mWechatpayHelper;

	private PayAgent(){

	}
	/*** 支付方式 */
	public enum PayType {
		/** 支付宝 */
		ALIPAY,
		/** 微信 */
		WECHATPAY,
		/** 银联支付 */
		UPPAY
	}
	
	private static volatile PayAgent instance;
	
	public static PayAgent getInstance() {

		if (null == instance) {

			synchronized (PayAgent.class) {

				if (null == instance) {
					instance = new PayAgent();
				}
			}
		}

		return instance;
	}
	
	public AlipayHelper getAlipayHelper() {
		if (null == mAlipayHelper) {
			mAlipayHelper = new AlipayHelper();
		}
		return mAlipayHelper;
	}

	public WechatPayHelper getWechatpayHelper() {
		if (null == mWechatpayHelper) {
			mWechatpayHelper = new WechatPayHelper();
		}
		return mWechatpayHelper;
	}

	/**
	* @Title: setDebug 
	* @Description: debug 模式
	* @param debug
	*/
	public void setDebug(boolean debug) {
		L.isDebug = debug;
	}
	
	/** 
	* @Title: setOnlieMode 
	* @Description: 设置 为正式环境或测试环境【true , false】
	* @param @param isOnlieMode    设定文件 
	*/
	public void setOnlieMode(boolean isOnlieMode) {
		getAlipayHelper().setOnlieMode(isOnlieMode);
		getWechatpayHelper().setOnlieMode(isOnlieMode);
	}
	
	/**
	 * 初始化 支付组件
	 * 
	 * @param activity
	 * @return void
	 * @autour BaoHong.Li
	 * @date 2015-7-16 下午5:04:36
	 * @update (date)
	 */
	public synchronized boolean initPay(Activity activity) {
		if (isInit) {
			return true;
		}

		boolean success = true;
		success &= ConstantKeys.initKeys(activity);
		//success &= getWechatpayHelper().registerWechatApi(activity);
		isInit = true;
		return success;
	}

	/**
	 * 支付
	 * @param ac
	 * @param payType 类型
	 * @param info    支付信息
	 * @param payListener 支付回调
	 */
	public void pay(Activity ac, PayType payType, PayInfo info, PayListener payListener){
		switch (payType) {
		case ALIPAY:
			getAlipayHelper().pay(ac,info, payListener);
			break;
		case WECHATPAY:
			getWechatpayHelper().pay(ac,info, payListener);
			break;
		case UPPAY:
			// 代接入
			break;

		default:
			break;
		}
	}

	/**
	 * 支付
	 * @param ac
	 * @param payType   类型
	 * @param orderNum  订单号
	 * @param price     价格
	 * @param subject   描述
	 * @param payListener
	 */
	public void pay(Activity ac, PayType payType, String orderNum, String price, String subject, PayListener payListener){
		PayAgent agent =  PayAgent.getInstance();
		PayInfo info =new PayInfo();
		info.setBody("商品");
		info.setNotifyUrl(notifyUrl);
		info.setNotifyIp(notifyIp);
		info.setSubject(subject);
		info.setFinalPrice(Double.parseDouble(price)); // 最多小数点2为 再多了会失败
		info.setOutTradeNo(orderNum);
		agent.pay(ac, payType, info, payListener);
	}
	/**
	 * 支付
	 * @param ac
	 * @param payType   类型
	 * @param orderNum  订单号
	 * @param price     价格
	 * @param payListener
	 */
	public void pay(Activity ac, PayType payType, String orderNum, String price, PayListener payListener){
		pay(ac,payType,orderNum,price,"商品",payListener);
	}
	/**
	 * *微信支付
	 * @param ac
	 * @param orderNum  订单号
	 * @param price     价格
	 * @param payListener
	 */
	public void payWeichat(Activity ac, String orderNum, String price, PayListener payListener){
		pay(ac, PayType.WECHATPAY,orderNum,price,payListener);
	}
	/**
	 * 支付宝支付
	 * @param ac
	 * @param orderNum  订单号
	 * @param price     价格
	 * @param payListener
	 */
	public void payAlipay(Activity ac, String orderNum, String price, PayListener payListener){
		pay(ac, PayType.ALIPAY,orderNum,price,payListener);
	}
}
